# window.py
import wx
import wx.aui
import wx.py
from pysage import ActorManager, Actor
from views.container import Container
from views.photoviewer import PhotoViewer
from views.albumviewer import AlbumViewer

omanager = ActorManager.get_singleton()

ALLOW_AUI_FLOATING = False

class Window(wx.Frame, Actor):
    '''the main frame that the application launches'''
    subscriptions = ['RevokeFocusFromViewerMessage']
    def __init__(self, parent, ID, title, pos=wx.DefaultPosition, size=wx.DefaultSize, 
                 # style=(wx.DEFAULT_FRAME_STYLE|wx.SUNKEN_BORDER|wx.NO_FULL_REPAINT_ON_RESIZE) & ~wx.RESIZE_BORDER & ~wx.RESIZE_BOX):
                 style=(wx.DEFAULT_FRAME_STYLE|wx.SUNKEN_BORDER|wx.NO_FULL_REPAINT_ON_RESIZE)):
        '''the main frame of the application'''
        wx.Frame.__init__(self, parent, ID, title, pos, size, style)
        Actor.__init__(self)
        self.uimanager = wx.aui.AuiManager()
        container = omanager.register_actor(Container(self), 'container')
        self.uimanager.SetManagedWindow(container)
        
        self.CenterOnScreen()
        
        # Prepare the menu bar
        menuBar = wx.MenuBar()

        # 1st menu from left
        menu1 = wx.Menu()
        # menu1.Append(103, "&Open", "Open a file")
        # menu1.AppendSeparator()
        menu1.Append(105, "&Add Folder", "Serves the content of a folder")
        # menu1.Append(106, "&Manage Folders", "get a list of folders")
        menu1.AppendSeparator()
        menu1.Append(104, "&Close", "Close the server")
 
        # Add menu to the menu bar
        menuBar.Append(menu1, "&File")
#        menu = wx.Menu()
#        menu.Append(2033, "&Add Viewer", "add a user to view the albums")
#        menu.Append(2034, '&Manage Viewers', 'get a list of current viewers')
#        # Append 2nd menu
#        menuBar.Append(menu, "&Users")
        
        menu = wx.Menu()
        menu.Append(3000, 'Toggle &Console')
#        self.onlineitem = menu.Append(3001, 'Online')
#        self.offlineitem = menu.Append(3002, 'Offline')
        
#        self.offlineitem.Enable(False)
        
        # menuBar.Append(menu, '&Settings')
        
        self.SetMenuBar(menuBar)
        
        self.Bind(wx.EVT_MENU, omanager.find('menupresenter').add_user, id=2033)
        self.Bind(wx.EVT_MENU, omanager.find('menupresenter').manage_viewers, id=2034)
        self.Bind(wx.EVT_MENU, omanager.find('menupresenter').add_folder, id=105)
        self.Bind(wx.EVT_MENU, omanager.find('menupresenter').manage_albums, id=106)
        self.Bind(wx.EVT_MENU, omanager.find('menupresenter').toggle_shell, id=3000)
        self.Bind(wx.EVT_MENU, omanager.find('menupresenter').close, id=104)
        self.Bind(wx.EVT_MENU, omanager.find('menupresenter').online, id=3001)
        self.Bind(wx.EVT_MENU, omanager.find('menupresenter').offline, id=3002)
        
        # Make the controls
#        self.cmd = wx.TextCtrl(self, -1, '''select a directory...''')
#        self.startBtn = wx.Button(self, -1, 'Online')
#        self.endBtn = wx.Button(self, -1, 'Offline')
#        self.endBn.Disable()
        
        left_pan = wx.Panel(container, style=wx.TAB_TRAVERSAL|wx.CLIP_CHILDREN) 
        self.albums_viewer = omanager.register_actor(AlbumViewer(left_pan, -1, size=(150, -1), style=wx.BORDER_SUNKEN), 'albumviewer')
        self.Bind(wx.EVT_LISTBOX, omanager.find('albumpresenter').on_album_select, self.albums_viewer)
#        self.search = wx.SearchCtrl(left_pan, size=(-1,-1), style=wx.TE_PROCESS_ENTER)
#        self.search.ShowCancelButton(1)
#        self.search.ShowSearchButton(1)
#        self.search.SetMenu( wx.Menu())
        
        left_box = wx.BoxSizer(wx.VERTICAL)
        left_box.Add(self.albums_viewer, 1, wx.EXPAND|wx.ALL)
#        left_box.Add(self.search, 0, wx.ALIGN_LEFT|wx.EXPAND|wx.ALL, 5)
        left_box.AddSpacer(5)
        left_pan.SetSizer(left_box)
        
        center_pan = wx.Panel(container, style=wx.TAB_TRAVERSAL|wx.CLIP_CHILDREN) 
        self.shell = wx.py.shell.Shell(center_pan, -1)
        self.shell.Hide()
        self.photos_viewer = omanager.register_actor(PhotoViewer(center_pan), 'photoviewer')
        
        self.photos_viewer.Bind(wx.EVT_KEY_DOWN, omanager.find('thumbnailpresenter').key_pressed)
        self.photos_viewer.Bind(wx.EVT_KEY_UP, omanager.find('thumbnailpresenter').key_released)
        # bind the game window mouse events to client
        self.photos_viewer.Bind(wx.EVT_LEFT_DOWN, omanager.find('thumbnailpresenter').mouse_pressed)
        self.photos_viewer.Bind(wx.EVT_LEFT_DCLICK, omanager.find('thumbnailpresenter').left_mouse_dclick)
        self.photos_viewer.Bind(wx.EVT_RIGHT_DOWN, omanager.find('thumbnailpresenter').right_mouse_pressed)
        self.photos_viewer.Bind(wx.EVT_LEFT_UP, omanager.find('thumbnailpresenter').mouse_released)
        self.photos_viewer.Bind(wx.EVT_RIGHT_UP, omanager.find('thumbnailpresenter').mouse_released)
        self.photos_viewer.Bind(wx.EVT_MIDDLE_UP, omanager.find('thumbnailpresenter').mouse_released)
        self.photos_viewer.Bind(wx.EVT_MIDDLE_DOWN, omanager.find('thumbnailpresenter').mouse_pressed)
        self.photos_viewer.Bind(wx.EVT_MOTION, omanager.find('thumbnailpresenter').mouse_motion)
        self.photos_viewer.Bind(wx.EVT_MOUSEWHEEL, omanager.find('thumbnailpresenter').mouse_wheel)
        self.photos_viewer.Bind(wx.EVT_ENTER_WINDOW, omanager.find('thumbnailpresenter').enter_window)
        self.photos_viewer.Bind(wx.EVT_LEAVE_WINDOW, omanager.find('thumbnailpresenter').leave_window)
#        self.photos_viewer.Bind(wx.EVT_SCROLL, omanager.find('thumbnailpresenter').mouse_wheel)
#        self.photos_viewer.Bind(wx.EVT_SCROLLWIN, omanager.find('thumbnailpresenter').mouse_wheel)

        self.Bind(wx.EVT_CLOSE, omanager.find('menupresenter').close)
        
        self.center_box = wx.BoxSizer(wx.VERTICAL)
        self.center_box.Add(self.photos_viewer, 1, wx.EXPAND|wx.ALL, 5)
        self.center_box.Add(self.shell, 0, wx.EXPAND|wx.ALL, 5)
        center_pan.SetSizer(self.center_box)
        # Hook up the events
#        self.Bind(wx.EVT_BUTTON, self.OnExecuteBtn, self.startBtn)
#        self.Bind(wx.EVT_BUTTON, self.OnEndBtn, self.endBtn)

#        # Do the layout
#        box1 = wx.BoxSizer(wx.HORIZONTAL)
#        box1.Add(self.cmd, 1, wx.ALIGN_CENTER|wx.LEFT|wx.RIGHT, 2)
#        box1.Add(self.startBtn, 0)
#        box1.Add(self.endBtn, 0)
#        
#
#        box2 = wx.BoxSizer(wx.HORIZONTAL) 
#        box2.Add(box3, 0, wx.EXPAND|wx.ALL, 2)
#        box2.Add(self.photos_viewer, 1, wx.EXPAND|wx.ALL, 2)
#        
#        sizer = wx.BoxSizer(wx.VERTICAL)
#        sizer.Add(box1, 0, wx.EXPAND|wx.ALL, 2)
#        sizer.Add(box2, 1, wx.EXPAND|wx.ALL, 2)
#
#        self.SetSizer(sizer)
#        self.SetAutoLayout(True)
        self.uimanager.AddPane(center_pan, wx.aui.AuiPaneInfo().CenterPane().Name("photos_viewer"))
        
        self.uimanager.AddPane(left_pan,
                         wx.aui.AuiPaneInfo().
                         Left().Layer(2).BestSize((170, -1)).
                         MinSize((160, -1)).
                         Floatable(ALLOW_AUI_FLOATING).FloatingSize((240, 700)).
                         Caption("Folders").
                         CloseButton(False).
                         Name("DemoTree"))     

        # self.CreateStatusBar(1, wx.ST_SIZEGRIP)
        self.uimanager.Update()
        self.uimanager.SetFlags(self.uimanager.GetFlags() ^ wx.aui.AUI_MGR_TRANSPARENT_DRAG)
    def handle_RevokeFocusFromViewerMessage(self, msg):
        self.SetFocus()
        return True
        
        
        